<template>
	<view class="detail-view">
		<view class="top-bar"><uni-navbar :fixed="true" :back="true" color="#fff" title="协商议事"></uni-navbar></view>

		<view class="tab-nav">
			<view class="nav-item" :class="navItem == 1 ? 'active' : ''">基本信息填写</view>
			<view class="nav-item" :class="navItem == 2 ? 'active' : ''">正文内容填写</view>
		</view>

		<view class="form-box">
			<u-form :model="form" ref="uForm">
				<block v-if="navItem == 1">
					<u-form-item class="form-item" required label="标题" prop="title"><u-input v-model="form.title" type="text" placeholder="请输入标题" /></u-form-item>
					<u-form-item class="form-item" required label="议题来源">
						<u-radio-group v-model="category" @change="categoryGroupChange" :key="category">
							<u-radio v-for="(item, index) in categoryList" :key="index" :name="item.name" :disabled="item.disabled">{{ item.label }}</u-radio>
						</u-radio-group>
					</u-form-item>

					<u-form-item class="form-item" required label="信息分类" prop="classify">
						<u-input v-model="classify" type="select" :select-open="classifyShow" placeholder="请选择类别" @click="classifyShow = true" />
						<u-select :list="classifyList" v-model="classifyShow" @confirm="classifyConfirm"></u-select>
					</u-form-item>

					<u-form-item class="form-item" required label="类别明细" prop="classifyDetails">
						<u-input v-model="type" type="select" :select-open="typeShow" placeholder="请选择类别" @click="typeShow = true" />
						<u-select :list="typeList" v-model="typeShow" @confirm="typeConfirm"></u-select>
					</u-form-item>
					<u-form-item class="form-item" required label="议事室" prop="roomName"><u-input v-model="roomName" type="select" :select-open="roomShow" placeholder="请选择议事室" @click="choseDiscuss" /></u-form-item>
					<u-form-item class="form-item" required label="联系人" prop="contact"><u-input v-model="form.contact" type="text" placeholder="请输入联系人" /></u-form-item>
					<u-form-item class="form-item" required label="联系电话" prop="phone"><u-input v-model="form.phone" type="text" placeholder="请输入联系电话" /></u-form-item>
				</block>
				<block v-else>
					<u-form-item class="form-item" label="议题背景"><u-input v-model="form.subBackground" type="textarea" placeholder="请输入议题背景" :clearable="false" :height="160" :maxlength="-1" /></u-form-item>
					<u-form-item class="form-item" label="上传文件"><uni-file-upload style="width: 500rpx"></uni-file-upload></u-form-item>
				</block>
			</u-form>
		</view>

		<view class="fiexd-btm">
			<u-button v-if="navItem == 1" type="error" @click="confirm">下一步</u-button>
			<u-button v-else type="error" @click="confirm">提交</u-button>
		</view>
	</view>
</template>

<script>
import uniFileUpload from '@/components/uni-file-uploader.vue';
import selectTree from '@/components/uni-select-tree.vue';
import { mapState } from 'vuex';
export default {
	components: {
		uniFileUpload,
		selectTree
	},
	data() {
		return {
			navItem: 1,
			roomName: '',
			roomShow: false,
			roomList: [],

			type: '',
			typeShow: false,
			typeList: [],

			classify: '',
			classifyShow: false,
			classifyList: [],

			category: 1,
			categoryList: [
				{
					name: 1,
					label: '党政点题'
				},
				{
					name: 2,
					label: '代表荐题'
				}
			],

			form: {
				title: '',
				issueSource: '1',
				classify: '',
				classifyDetails: '',
				roomId: '',
				roomName: '',
				roomPId: '',
				roomType: '',
				reflectionArea: '',
				contact: '',
				phone: '',
				subBackground: ''
			},
			rules: {
				title: [
					{
						required: true,
						message: '请输入标题',
						trigger: 'change'
					}
				],
				type: [
					{
						required: true,
						message: '请选择类别',
						trigger: 'change'
					}
				],
				contact: [
					{
						required: true,
						message: '请选择联系人',
						trigger: 'change'
					}
				],
				phone: [
					{
						required: true,
						message: '请选择联系电话',
						trigger: 'change'
					}
				],
				classify: [
					{
						required: true,
						message: '请选择信息分类',
						trigger: 'change'
					}
				],
				classifyDetails: [
					{
						required: true,
						message: '请选择类型明细',
						trigger: 'change'
					}
				],
				roomName: [
					{
						required: true,
						message: '请选择议事室',
						trigger: 'change'
					}
				],
				subBackground: [
					{
						required: true,
						message: '请选择议题背景',
						trigger: 'change'
					}
				]
			}
		};
	},
	onLoad() {
		this.loadLogin();
		this.loadDict('sqmy_category', 'classifyList');
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
		uni.setStorageSync('roomMsg', {});
	},
	onShow() {
		this.roomConfirm(uni.getStorageSync('roomMsg'));
	},
	onUnload() {
		this.$store.commit('filePush', []);
	},
	computed: {
		...mapState(['userInfo', 'fileList'])
	},
	methods: {
		choseDiscuss() {
			uni.navigateTo({
				url: '/pagesA/assembly/discuss'
			});
		},
		treeConfirm(e, type) {
			if (!e || e.length == 0) {
				this[type + 'Show'] = false;
				this[type + 'Name'] = '';
				this[type + 'Ids'] = [];
				return false;
			}
			this[type + 'Ids'] = [];
			let names = [];
			let i = 0;
			e.forEach((item) => {
				this[type + 'Ids'].push(item.id);
				if (i < 3) {
					names.push(item.name);
					i++;
				}
			});
			this[type + 'Name'] = names.join(',') + '等' + e.length + '人';
			this[type + 'Show'] = false;
		},
		treeCancel(e, type) {
			this[type + 'Show'] = false;
		},
		categoryGroupChange(e) {
			this.form.issueSource = e;
		},
		classifyConfirm(e) {
			this.classify = e[0].label;
			this.form.classify = e[0].value;
			this.loadDict(this.form.classify, 'typeList');
			this.type = '';
			this.form.classifyDetails = null;
		},
		typeConfirm(e) {
			this.type = e[0].label;
			this.form.classifyDetails = e[0].value;
		},
		roomConfirm(e) {
			this.roomName = e.roomName;
			this.form.roomId = e.id;
			this.form.reflectionArea = e.roomArea;
			this.form.roomName = e.roomName;
			this.form.roomPId = e.roomArea;
			this.form.roomType = e.roomType;
		},
		loadLogin() {
			if (this.userInfo.Authorization) {
				// console.log(this.userInfo);
				this.clientCode = this.userInfo.sysUser.clientCode;

				this.form.clientCode = this.userInfo.sysUser.clientCode;
				this.form.contact = this.userInfo.sysUser.userName;
				this.form.submitName = this.userInfo.sysUser.userName;
				this.form.createBy = this.userInfo.sysUser.id;
				this.form.submitBy = this.userInfo.sysUser.id;
				this.form.phone = this.userInfo.sysUser.phone;
			} else {
				this.$logout();
			}
		},
		loadDict(type, list) {
			this.$request('/system/sys-dict/dictList', {
				clientCode: this.clientCode,
				sysDict: {
					dictType: type
				}
			}).then((res) => {
				if (!res.data) {
					return false;
				}
				this[list] = [];
				res.data.forEach((item) => {
					this[list].push({
						value: item.dictValue,
						label: item.dictName
					});
				});
			});
		},
		onEditorReady(edit) {
			this.$refs.editor
				.context((res) => {
					this[edit] = res.context;
				})
				.exec();
		},
		confirm() {
			if (this.navItem == 1) {
				this.$refs.uForm.validate((valid) => {
					if (!valid) {
						return false;
					} else {
						this.navItem = 2;
					}
				});
			} else {
				this.$refs.uForm.validate((valid) => {
					if (!valid) {
						return false;
					} else {
						let data = {};

						data = {
							flag: 'add',
							info: this.form,
							fileList: this.fileList
						};
						this.$request('/xieshangyishi/info/add', data, {
							load: true
						}).then((res) => {
							this.$msg(res.msg);
							setTimeout(() => {
								let pre = this.$prePage();
								if (pre && pre.loadData) {
									pre.loadData('refresh');
								}
								uni.navigateBack();
							}, 500);
						});
					}
				});
			}
		}
	}
};
</script>

<style lang="scss">
.detail-view {
	background-color: #fff;
}

.form-box {
	.form-item {
		@include mp-flex;
		border-bottom: 20rpx solid $u-border-color;
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}
</style>
